package com.xmwzw.catpu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.IAdapter;
import com.mikepenz.fastadapter.IItem;
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.xmwzw.catpu.bean.CollectionEvent;
import com.xmwzw.catpu.bean.SoftBean;
import com.xmwzw.catpu.databinding.ActivityCollectBinding;
import com.xmwzw.catpu.db.SoftModel;
import com.xmwzw.catpu.db.SoftModel_Table;
import com.xmwzw.catpu.item.SoftItem;
import com.xmwzw.catpu.util.Constant;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangzw
 * @date : 20-4-3下午5:15
 * @desc :
 */
public class MyCollectActivity extends BaseActivity {
    private ActivityCollectBinding collectBinding;
    private FastItemAdapter fastItemAdapter = new FastItemAdapter();
    private List<SoftItem> items = new ArrayList<>();
    @Override
    public int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        collectBinding = getBaseViewBinding();
        if (!EventBus.getDefault().hasSubscriberForEvent(CollectionEvent.class)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        items.clear();
        fillData();
    }


    private void fillData() {
        List<SoftModel> softModels = SQLite.select().from(SoftModel.class).where(SoftModel_Table.isCollect.eq(true)).queryList();
        for (int i = 0; i < softModels.size(); i++) {
            SoftBean softBean = new SoftBean();
            SoftModel softModel = softModels.get(i);
            softBean.name = softModel.name;
            softBean.version = softModel.version;
            softBean.icon_url = softModel.icon_url;
            softBean.down_url = softModel.down_url;
            softBean.id = softModel.id;
            items.add(new SoftItem().fillData(false,softBean));
        }
        fastItemAdapter.set(items);
        collectBinding.rvCollect.setLayoutManager(new LinearLayoutManager(this));
        collectBinding.rvCollect.setAdapter(fastItemAdapter);

        fastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener() {
            @Override
            public boolean onClick(View v, IAdapter adapter, IItem item, int position) {
                SoftItem item1 = (SoftItem) item;
                Intent intent = new Intent(MyCollectActivity.this, SoftDetailActivity.class);
                intent.putExtra(Constant.KEY_SOFT_BEAN, item1.getSoftBean());
                startActivity(intent);
                return false;
            }
        });
    }
}
